const fs=require('fs');
const path=require('path');
const http=require('http');
const argv=process.argv;

http.createServer((req,res)=>{
    res.writeHead(200,{'content-type':'text/html;charset=utf-8'});
    if(argv[2]){
        var filePath=path.join(__dirname,'/'+argv[2]);
        fs.access(filePath,(err)=>{
            if(err){
                console.log(err);
                res.end();
            }else{
                var readable=fs.createReadStream(filePath);
                readable.pipe(res);
                res.on('end',()=>{
                    console.log('read ended!');
                })
            }
        })
    }else{
        var filePath=__filename;
        var readable=fs.createReadStream(filePath);
        readable.pipe(res);
        res.on('end',()=>{
            console.log('read ended!');
        })
    }
}).listen(8081);